﻿using ComTools.Helpers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace ComTools.DesignPattern.ProcessingPipeline
{
    class Step2: AProcessingStep<HttpRequestMessage, object, string>
    {
        dynamic userBaseInfo;

        /// <summary>
        /// 根据code拉取用户信息
        /// </summary>
        /// <param name="parameter">HttpRequestMessage</param>
        public override void ProcessCurrentStep(HttpRequestMessage parameter)
        {
            // 获得code参数
            string code = parameter
                            .GetQueryNameValuePairs()
                            .ToList()
                            .Where(kv => kv.Key == "code")
                            .FirstOrDefault()
                            .Value;

            // 设置获取用户信息的accesstoken和openid的url
            string userAuthUrl = "";

            // 请求获取用户信息的accesstoken和openid
            var userAuthInfo = "";// SendGetRequestAsStringAsync(userAuthUrl);

            if (userAuthInfo.IndexOf("errcode") < 0)
            {
                dynamic dyUserAuthInfo = "";// JsonHelper.ToDynamicObject(userAuthInfo);

                // 组装获取用户数据的url
                var getUserInfoUrl = ""; // string.Format(
                                         //                    ThirdPartyConfig.config.weixin.wxH5AuthLogin.getWeixinAccountInfoUrl,
                                         //                    dyUserAuthInfo.access_token,
                                         //                    dyUserAuthInfo.openid);
                // 拉取用户数据
                userBaseInfo = "";// SendGetRequestAsDynamicJsonResponseAsync(getUserInfoUrl);

                ProcessNextStep(parameter);
            }
        }

        /// <summary>
        /// 下一步，用户数据的操作，到数据库校验是否有此用户，并返回用户的id
        /// </summary>
        /// <param name="parameter"></param>
        public override void ProcessNextStep(HttpRequestMessage parameter)
        {
            //var next = new Step3;
            //next.LastStepProccessResult = userBaseInfo;
            //next.ReturnResultAction = ReturnResultAction;
            //next.ProcessCurrentStep(parameter);
        }
    }
}
